//
//  JNavigationTo.m
//  jhf
//
//  Created by 计海峰 on 2017/9/7.
//  Copyright © 2017年 计海峰. All rights reserved.
//

#import "JNavigationTo.h"
#import <MapKit/MapKit.h>

@implementation JNavigationTo

JSingletonM();


- (void)navigationWithPOI:(JLocationModel *)poi;{
    
    _poi = poi;
    
    NSArray *array = [NSArray arrayWithObjects:FloatToSting(poi.latitude), FloatToSting(poi.longitude), nil];
    [self doNavigationWithEndLocation:array];
    
}


//导航只需要目的地经纬度，endLocation为纬度、经度的数组
-(void)doNavigationWithEndLocation:(NSArray *)endLocation
{
    
    //NSArray * endLocation = [NSArray arrayWithObjects:@"26.08",@"119.28", nil];
    
    //本机上面装的地图
    NSMutableArray *maps = [NSMutableArray array];
    
    //苹果原生地图-苹果原生地图方法和其他不一样
    NSMutableDictionary *iosMapDic = [NSMutableDictionary dictionary];
    iosMapDic[@"title"] = @"苹果地图";
    [maps addObject:iosMapDic];
    
    NSString *name = self.poi.name?self.poi.name:@"目的地";
    //百度地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
        NSMutableDictionary *baiduMapDic = [NSMutableDictionary dictionary];
        baiduMapDic[@"title"] = @"百度地图";
        // 也可以定义出发地址
        //NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%@,%@|name:我的位置&destination=latlng:39.915,120.202|name:终点&mode=driving", endLocation[0], endLocation[1] ] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;
        NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}|name:我的位置&destination=latlng:%@,%@|name:%@&mode=driving", endLocation[0], endLocation[1], name] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        baiduMapDic[@"url"] = urlString;
        [maps addObject:baiduMapDic];
    }
    
    //高德地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
        NSMutableDictionary *gaodeMapDic = [NSMutableDictionary dictionary];
        gaodeMapDic[@"title"] = @"高德地图";
        NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%@&lon=%@&dev=0&style=2",@"钱龙图",@"ytk",endLocation[0],endLocation[1]] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        gaodeMapDic[@"url"] = urlString;
        [maps addObject:gaodeMapDic];
    }
    
    //谷歌地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
        NSMutableDictionary *googleMapDic = [NSMutableDictionary dictionary];
        googleMapDic[@"title"] = @"谷歌地图";
        NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%@,%@&directionsmode=driving",@"导航",@"nav123456",endLocation[0], endLocation[1]] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        googleMapDic[@"url"] = urlString;
        [maps addObject:googleMapDic];
    }
    
    //腾讯地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {
        NSMutableDictionary *qqMapDic = [NSMutableDictionary dictionary];
        qqMapDic[@"title"] = @"腾讯地图";
        NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?from=我的位置&type=drive&tocoord=%@,%@&to=%@&coord_type=1&policy=0",endLocation[0], endLocation[1], name] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        qqMapDic[@"url"] = urlString;
        [maps addObject:qqMapDic];
    }
    
    NSMutableArray <NSString *>* titleStrings = [NSMutableArray array];
    [maps enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [titleStrings addObject:[obj objectForKey:@"title"]];
    }];
    
    MJWeakSelf;
    [[LCActionSheet sheetWithTitle:@"选择地图" cancelButtonTitle:@"取消" didDismiss:^(LCActionSheet * _Nonnull actionSheet, NSInteger buttonIndex) {
        
        if (buttonIndex == 0) {
            return ;
        }
        
        buttonIndex -= 1;
        
        //苹果原生地图方法
        if (buttonIndex == 0) {
            [weakSelf navAppleMap];
        }
        
        //开始导航
        if (buttonIndex <= maps.count) {
            NSString *urlString = maps[buttonIndex][@"url"];
            dispatch_async(dispatch_get_main_queue(), ^{
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {}];
            });
            //            [[UIApplication sharedApplication] openURL:options:completionHandler::];
        }
        
    } otherButtonTitleArray:titleStrings] show];
    
    
}

//苹果地图
- (void)navAppleMap
{
    //终点坐标
    CLLocationCoordinate2D loc = CLLocationCoordinate2DMake(_poi.latitude, _poi.longitude);
    
    //用户位置
    MKMapItem *currentLoc = [MKMapItem mapItemForCurrentLocation];
    
    //终点位置
    MKMapItem *toLocation = [[MKMapItem alloc]initWithPlacemark:[[MKPlacemark alloc]initWithCoordinate:loc addressDictionary:nil] ];
    toLocation.name = self.poi.name?self.poi.name:@"目的地";
    NSArray *items = @[currentLoc,toLocation];
    //第一个
    NSDictionary *dic = @{
        MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
        MKLaunchOptionsMapTypeKey : @(MKMapTypeStandard),
        MKLaunchOptionsShowsTrafficKey : @(YES)
    };
    //第二个，都可以用
    //    NSDictionary * dic = @{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,
    //                           MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]};
    
    [MKMapItem openMapsWithItems:items launchOptions:dic];
    
    
}
@end
